/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.component.cim.handler;

import com.farsunset.cim.constant.ChannelAttr;
import com.farsunset.cim.handler.CIMRequestHandler;
import com.farsunset.cim.model.ReplyBody;
import com.farsunset.cim.model.SentBody;
import com.workcell.workdata.im.component.cim.handler.annotation.CIMHandler;
import com.workcell.workdata.im.service.SessionService;
import io.netty.channel.Channel;
import org.springframework.http.HttpStatus;

import javax.annotation.Resource;

/**
 * 记录客户端上传的位置信息，用于需要使用位置的相关服务
 */
@CIMHandler(key = "client_location")
public class LocationHandler implements CIMRequestHandler {

	@Resource
	private SessionService sessionService;
	 
	@Override
	public void process(Channel channel, SentBody body) {

		ReplyBody reply = new ReplyBody();
		reply.setKey(body.getKey());
		reply.setCode(HttpStatus.OK.value());
		reply.setTimestamp(System.currentTimeMillis());

		Long uid = Long.parseLong(body.get("uid"));

		String nid = channel.attr(ChannelAttr.ID).get();

		double longitude = Double.parseDouble(body.get("longitude","0"));

		double latitude = Double.parseDouble(body.get("latitude","0"));

		String location = body.get("location");

		sessionService.updateLocation(uid,nid,longitude,latitude,location);

		channel.writeAndFlush(reply);
	}
}
